import React, {Component} from 'react';
import './index.css'

class Item extends Component {
    state = {mouse: false}
    //鼠标移出移入
    handMouse = (flag) => {
        return () => {
            this.setState({mouse: flag})
        }
    }

    //勾选/取消勾选
    handCheck = (id) => {
        return (event) => {
            this.props.changeTodo(id, event.target.checked)
        }
    }

    deleteTodo = (id) => {
        if (window.confirm('确定删除?')){
            this.props.deleteTodo(id)
        }
    }

    render() {
        let {id, name, done} = this.props;
        let {mouse} = this.state;
        return (
            <li style={{backgroundColor: mouse ? '#ddd' : '#fff'}}
                onMouseLeave={this.handMouse(false)}
                onMouseEnter={this.handMouse(true)}>
                <label>
                    <input checked={done}
                           onChange={this.handCheck(id)}
                           type="checkbox"/>
                    <span>{name}</span>
                </label>
                <button onClick={() => this.deleteTodo(id)} className="btn btn-danger"
                        style={{display: mouse ? 'block' : 'none'}}>删除
                </button>
            </li>
        );
    }
}

export default Item;